﻿using MediatR;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.SheepFile
{
    public class ProgenyFileCreateHandler : IRequestHandler<ProgenyFileCreateCommon, Result>
    {
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        public readonly IBaseRepository<BirthMessage> BirthMessageRepository;
        public readonly IBaseRepository<ProgenyFile> ProgenyFileRepository;

        public ProgenyFileCreateHandler(IBaseRepository<BasicInformation> basicInformationRepository, IBaseRepository<BirthMessage> birthMessageRepository, IBaseRepository<ProgenyFile> progenyFileRepository)
        {
            BasicInformationRepository = basicInformationRepository;
            BirthMessageRepository = birthMessageRepository;
            ProgenyFileRepository = progenyFileRepository;
        }

        public async Task<Result> Handle(ProgenyFileCreateCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
               var list= BasicInformationRepository.GetAll().Where(x => x.Auricle == request.ProgenyAuricle).ToList();
                if (list.Count() == 0)
                {
                    var result = new BasicInformation()
                    {
                        Auricle = request.ProgenyAuricle,
                        SheepbreedId = request.SheepbreedId,
                        CorpuscleAuricle = request.ProgenyCorpuscleAuricle,
                        GeneLV = request.GeneLV,
                        SheepTypeId = 3,
                        SheepSex = request.ProgenySex,
                        BuildingId = 0,
                        ColumnsId = 0,
                        SheepStatus = 0
                    };
                    BasicInformationRepository.Add(result);
                    var result2 = new BirthMessage()
                    {
                        BreedingSheepArchiveId = result.BreedingSheepArchiveId,
                        DadBreedingSheepArchiveId = request.DadBreedingSheepArchiveId,
                        MomBreedingSheepArchiveId = request.MomBreedingSheepArchiveId,
                        BirthDate = request.BirthDate,
                        AdmissionDate = request.EntranceDate,
                        BirthAdd = request.BirthAddreess,
                        BirthWeight = request.BirthWeight,
                        Remark = request.Remark,
                        BirthStatusId = request.BirthStatusId
                    };
                    BirthMessageRepository.Add(result2);
                    var result1 = new ProgenyFile()
                    {
                        BreedingSheepArchiveId = result.BreedingSheepArchiveId,
                        ProgenyAuricle = request.ProgenyAuricle,
                        ProgenyCorpuscleAuricle = request.ProgenyCorpuscleAuricle,
                        DadBreedingSheepArchiveId = request.DadBreedingSheepArchiveId,
                        MomBreedingSheepArchiveId = request.MomBreedingSheepArchiveId,
                        SheepbreedId = request.SheepbreedId,
                        ProgenySex = request.ProgenySex,
                        BirthStatusId = request.BirthStatusId,
                        BirthDate = request.BirthDate,
                        EntranceDate = request.EntranceDate,
                        BirthAddreess = request.BirthAddreess,
                        BirthWeight = request.BirthWeight,
                        GeneLV = request.GeneLV,
                        Remark = request.Remark,
                        SheepTypeId = 3
                    };
                    ProgenyFileRepository.Add(result1);

                    res.Code = (int)CommonEnumCode.success;
                    res.Msg = "羊只档案基本信息表，后裔基本表新增";
                }
                else
                {
                    res.Code = (int)CommonEnumCode.error;
                    res.Msg = "后裔耳号已存在";
                }
                
            }
            catch (Exception)
            {

            }
            return res;
        }
    }
}
